package web.java.f_batch;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.junit.Test;

import web.java.z_JDBCUtil.DBUtil;

public class Demo01_ProcessingBatch {
	/**
	 * processing batch 批处理
	 * */
	/**
	 * |- statement方法
	 * */
	//全局变量
	private Connection conn;
	private PreparedStatement pstmt;
	private ResultSet result;
	
	@Test
	public void processBatch(){
		conn=DBUtil.connection();
		try{
			String sql="INSERT INTO USER (NAME,permission_id,deposit) VALUES(?,?,?)";
			pstmt=conn.prepareStatement(sql);
			for(int i=1;i<=2000;i++){
				pstmt.setString(1,"user"+i);
				pstmt.setInt(2,2);
				pstmt.setInt(3,2000);
				
				pstmt.executeUpdate();	
				//添加批处理 两种方式时间差不多
				/*pstmt.addBatch();
				if(i%500==0){
					pstmt.executeBatch();
					pstmt.clearBatch();
				}
				
				//批量执行
				pstmt.executeUpdate();
				pstmt.clearBatch();*/
			}
			DBUtil.closeConn(conn, pstmt);
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
	
}
